define(function() {
	var frameScript = {
		//异步请求
		ajax: function(config) {
		
			config = config || {};
			config.method = config.method || "GET";
			config.method = config.method.toUpperCase();
			config.url = config.url || "localhost";
			config.async = typeof config.async === "undefined" ? true : config.async;
			config.data = config.data || {};
			

			var dataStr = "";
			
			
			var xhr = new XMLHttpRequest();
			xhr.withCredentials = true;
			xhr.open(config.method, config.url, config.async);
			xhr.timeout = 20000;		//超过20s  请求超时
			console.log(config.data.toString())
			if(config.data.toString() == "[object FormData]"){
				
				dataStr = config.data;
				
			}else if(typeof config.data === "object") {
				
				for(var i in config.data) {
					
					dataStr += ("&" + i + "=" + config.data[i]);
				}
				
				dataStr = dataStr.substr(1, dataStr.length);
				xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			}
			
			xhr.onreadystatechange = function() {
				//成功状态
				if(xhr.readyState == 4 && xhr.status == 200) {
					if(typeof config.success === "function") {
						config.success(JSON.parse(xhr.responseText));
					}
				
				//失败状态
				}else if(xhr.readyState == 4 && xhr.status == 404) {
					if(typeof config.error === "function") {
						config.error({
							status:xhr.status,
							message:'找不到相应链接'
						});
					}
				
				//没有连接网络或者请求超时
				} else if(xhr.readyState == 4 && xhr.status == 0){
					if(typeof config.error === "function") {
						config.error({
							status:xhr.status,
							message:'网络连接失败'
						});
					}
//					QXB_CALLBACK.PC_Chat_index.networkError();
				
				//其他错误返回当前xhr对象
				}else if(xhr.readyState == 4 && xhr.status != 200){
					if(typeof config.error === "function") {
						config.error(xhr);
					}
				}
			};
			xhr.send(dataStr);
			
		},
		addEventListener: function(obj, eventName, func, capture) {
			var capture = capture || false;
			obj.addEventListener(eventName, func, capture);
			return function() {
				obj.removeEventListener(eventName, func, capture);
			}
		},
		hasClass: function(obj, cls) {
			if(obj === null || typeof obj === "undefined") {
				return false;
			}
			return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
		},
		
		isEmptyObject:function(e) {  
		    var t;  
		    for (t in e)  
		        return 1;  
		    return 0  
		},

		addClass: function(obj, cls) {
			if(obj === null || typeof obj === "undefined") {
				return false;
			}
			if(!this.hasClass(obj, cls)) obj.className += " " + cls;
		},

		removeClass: function(obj, cls) {
			if(obj === null || typeof obj === "undefined") {
				return false;
			}
			if(this.hasClass(obj, cls)) {
				var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
				obj.className = obj.className.replace(reg, '');
			}
		},
		//获取地址栏参数的值
		getQueryString: function(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r != null) return unescape(r[2]);
			return null;
		},
		
		//将一个链接字符串后面的参数转换为对象
		parseQueryString:function (url){
		    var arr;
		    var res = {};
		    //#符号之后的值称为hash，都不会加到request请求中去
		    url = url.split('#')[0];
		    //获取queryString 第一个？号后面的全是查询字符串
		    arr = url.split('?');
		    arr.shift();
		    var queryStr = arr.join('?');
		    //查询字符串为空直接返回 避免出现这样的返回值{"":""}
		    if (queryStr.trim().length == 0){
		        return res;
		    }
		
		    //获取参数
		    arr = queryStr.split('&');
		    for (var i = 0; i <  arr.length; i++) {
		        var itemArr = arr[i].split('=');
		        //第一个=号之前的是name 后面的全是值
		        var name = itemArr.shift();
		        var value = itemArr.join('=');
		        res[name] = value;
		    }
		    return res;
		},
		
		parseData: function(obj) {
			if(typeof obj === "object") {
				var dataStr = "";
				for(var i in obj) {
					dataStr += ("&" + i + "=" + obj[i]);
				}
				dataStr = dataStr.substr(1, dataStr.length);
				return dataStr;
			}
			return obj;
		},
		//页面滚动开关
		scrollDisable: function() {
			document.addEventListener("touchmove", this.scrollEventHandle, false);
		},
		scrollEnable: function() {
			document.removeEventListener("touchmove", this.scrollEventHandle, false);
		},
		scrollEventHandle: function() {
			event.preventDefault();
		},
		//页面滚动控制,下拉刷新与上拉加载更多
		pageScrollInit: function() {
			var page = document.querySelectorAll(".ngPage");
			setTimeout(function() {
				page[page.length - 1].className += " loaded";
			}, 100);

			function scrollTo1() {
				this.scrollTop = 1;
				this.removeEventListener("webkitTransitionEnd", scrollTo1);
			}
			page[page.length - 1].addEventListener("webkitTransitionEnd", scrollTo1);
			var bannerHeight = 0;
			if(document.getElementById("banner")) {
				bannerHeight = document.getElementById("banner").offsetHeight;
			}

			var hash = window.location.hash;
			page[page.length - 1].addEventListener("scroll", function(e) {
				if(e.target.hasAttribute("ng-view")) {
					if(e.target.scrollTop === 0) {
						this.scrollTop = 1;
					}
					if(e.target.scrollTop + e.target.offsetHeight === e.target.scrollHeight) {
						this.scrollTop = e.target.scrollTop - 1;
					}
					//首页的搜索条透明度处理
					if(hash === "#/main") {
						if(bannerHeight === 21) {
							bannerHeight = document.getElementById("banner").offsetHeight;
						} else {
							if(e.target.scrollTop + 44 >= bannerHeight) {
								document.getElementById("indexSearchBar").className = "";
							} else {
								document.getElementById("indexSearchBar").className = "transparent";
							}
						}
					}
				}
			});
		},
		//DIV滚动控制,下拉刷新与上拉加载更多
		divScrollInit: function(selectorStr) {
			var page = document.querySelectorAll(selectorStr);
			setTimeout(function() {
				page[page.length - 1].scrollTop = 1;
			}, 0);

			page[page.length - 1].addEventListener("scroll", function(e) {
				if(e.target.hasAttribute("initScroll")) {
					if(e.target.scrollTop === 0) {
						this.scrollTop = 1;
					}
					if(e.target.scrollTop + e.target.offsetHeight === e.target.scrollHeight) {
						this.scrollTop = e.target.scrollTop - 1;
					}
				}
			});
		},
		//主tabBar控制
		showIndexTabBar: function() {
			document.getElementById("indexTabBar").className = "";
		},
		hideIndexTabBar: function() {
			document.getElementById("indexTabBar").className = "hide";
		},
		setCurrentIcon: function(ele) {
			var buttons = document.querySelectorAll("#indexTabBar button");
			for(var i = 0; i < buttons.length; i++) {
				this.removeClass(buttons[i], "current");
			}
			this.addClass(ele, "current");
		},
		//主搜索条控制
		showIndexSearchBar: function() {
			var hash = window.location.hash;
			if(hash === "#/main") {
				document.getElementById("indexSearchBar").className = "transparent";
			} else {
				document.getElementById("indexSearchBar").className = "";
			}
			document.getElementById("shopSearchBar").className = "hide";
		},
		hideIndexSearchBar: function() {
			if(document.getElementById("indexSearchBar").className === "transparent") {
				document.getElementById("indexSearchBar").className = "transparent hide";
			} else {
				document.getElementById("indexSearchBar").className = "hide";
			}
			document.getElementById("shopSearchBar").className = "hide";
		},
		showShopSearchBar: function() {
			document.getElementById("shopSearchBar").className = "";
		},
		//修改浏览器标题
		setTitle: function(title) {
			document.title = title;
			// 判断是否为ios设备，ios设备需要通过加载iframe来刷新title
			if(navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
				document.getElementById("iframeForSetTitle").src = '/blank.png?v=' + Math.random()
			}
		},
		//合并对象
		/* des - 合并到的对象,类型:对象
		 * src - 合并源,类型:数组,如[{a:1},{b:2}]
		 * override - 冲突是否覆盖,类型:布尔值
		 */
		extend: function(des, src, override) {
			if(src instanceof Array) {
				for(var i = 0, len = src.length; i < len; i++) {
					this.extend(des, src[i], override);
				}
			} else {
				for(var i in src) {
					if(override || !(i in des)) {
						des[i] = src[i];
					}
				}
			}
			return des;
		},
		/*
		 获取cookie
		 * */
		getCookie: function(c_name) {
			if(document.cookie.length > 0) {
				c_start = document.cookie.indexOf(c_name + "=")
				if(c_start != -1) {
					c_start = c_start + c_name.length + 1
					c_end = document.cookie.indexOf(";", c_start)
					if(c_end == -1) c_end = document.cookie.length
					return unescape(document.cookie.substring(c_start, c_end))
				}
			}
			return ""
		},
		/*
		 得到日期
		 * */
		myTime:function (unixTime, isFull, timeZone) {
             if (typeof (timeZone) == 'number') {
                 unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
             }
             var time = new Date(unixTime * 1000);
             var ymdhis = "";
             ymdhis += time.getUTCFullYear() + "-";

             ymdhis += (time.getUTCMonth() + 1) < 10 ? '0' + (time.getUTCMonth() + 1) + "-" : (time.getUTCMonth() + 1) + "-";
             ymdhis += time.getUTCDate() < 10 ? '0' + time.getUTCDate() : time.getUTCDate();
             if (isFull === true) {
                 ymdhis += " " + time.getUTCHours() < 10 ? " " + '0' + time.getUTCHours() + ":" : " " + time.getUTCHours() + ":";
                 ymdhis += time.getUTCMinutes() < 10 ? '0' + time.getUTCMinutes() + ":" : time.getUTCMinutes() + ":";
                 ymdhis += time.getUTCSeconds() < 10 ? '0' + time.getUTCSeconds() : time.getUTCSeconds();
             }
             return ymdhis;
       },
       
       	//验证手机号
	  	checkPhone:function(mobile){ 
		  	let bool = true;
		    if(!(/^1[34578]\d{9}$/.test(mobile))){ 
		        return false; 
		    }
		    return bool;
		},
       
       startMove: function(obj,json,endFn){
       	
       		clearInterval(obj.timer);		
			obj.timer = setInterval(function(){
				
				var bBtn = true;
				
				for(var attr in json){
					
					var iCur = 0;
				
					if(attr == 'opacity'){
						if(Math.round(parseFloat(frameScript.getStyle(obj,attr))*100)==0){
						iCur = Math.round(parseFloat(frameScript.getStyle(obj,attr))*100);
						
						}
						else{
							iCur = Math.round(parseFloat(frameScript.getStyle(obj,attr))*100) || 100;
						}	
					}
					else{
						iCur = parseInt(frameScript.getStyle(obj,attr)) || 0;
					}
					
					var iSpeed = (json[attr] - iCur)/2;
					iSpeed = iSpeed >0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
					if(iCur!=json[attr]){
						bBtn = false;
					}
					
					if(attr == 'opacity'){
						obj.style.filter = 'alpha(opacity=' +(iCur + iSpeed)+ ')';
						obj.style.opacity = (iCur + iSpeed)/100;
						
					}
					else{
						obj.style[attr] = iCur + iSpeed + 'px';
					}
					
					
				}
				
				if(bBtn){
					clearInterval(obj.timer);
					
					if(endFn){
						endFn.call(obj);
					}
				}
				
			},5);
      },
      getStyle:function(obj,attr){
      	if(obj.currentStyle){
			return obj.currentStyle[attr];
		}
		else{
			return getComputedStyle(obj,false)[attr];
		}
      },
		
		
		//校验vin号
		check_vin:function (vin){
			var char_dict = {A:1,B:2,C:3,D:4,E:5,F:6,G:7,H:8,J:1,K:2,L:3,M:4,N:5,P:7,R:9,S:2,T:3,U:4,V:5,W:6,X:7,Y:8,Z:9,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,0:0};
			var char_ratio = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2];
			var vin_chars =  "ABCDEFGHJKLMNPRSTUVWXYZ1234567890";
	        var check_code = '';
	        var is_in_char   = false;
	        var vinArr = vin.split('');
			if (vinArr.length == 17){
	            var sum = 0;
	            for (i = 0; i < vinArr.length; i++){
	                if(vin_chars.indexOf(vinArr[i]) != -1){
	                    is_in_char = true;
	                    sum = sum + parseInt(char_dict[vinArr[i]]) * parseInt(char_ratio[i]);
	                }else{
	                    is_in_char = false;
	                    break;
	                }
	            }
	            if (is_in_char){
	                var intTemp = sum % 11;
	                if (intTemp == 10){
	                    check_code = "X";
	                }else{
	                    check_code = intTemp;
	                }
	            }
	        }
	        if (check_code == vinArr[8]) return true;
	        return false;
	   	},
	   	
	   	urlToObject:function(urlString) { 
			var urlObject = {};  
			var urlArray = urlString.split("&"); 
			for (var i=0; i < urlArray.length;i++) { 
				var urlItem = urlArray[i]; 
				var item = urlItem.split("="); 
				urlObject[item[0]] = item[1];
			}
			return urlObject; 
			
		},
	   	
	   	//图片压缩
	   	compress(img) {

            //img.crossOrigin = "Anonymous";
	        var initSize = img.src.length,
		        width = img.width,
		        height = img.height,
				ratio,
				canvas = document.createElement('canvas'),
				count;

	        //如果图片大于四百万像素，计算压缩比并将大小压至400万以下
	        if ((ratio = width * height / 4000000)>1) {
	            ratio = Math.sqrt(ratio);
	            width /= ratio;
	            height /= ratio;
	        }else {
	            ratio = 1;
	        }

	        canvas.width = width;
	        canvas.height = height;
			var ctx = canvas.getContext('2d');
	//        铺底色
	        ctx.fillStyle = "#fff";
	        ctx.fillRect(0, 0, canvas.width, canvas.height);

	            ctx.drawImage(img, 0, 0, width, height);
	        var ndata = canvas.toDataURL("image/jpeg", 0.5);
	
	        console.log("压缩前：" + initSize);
	        console.log("压缩后：" + ndata.length);
	        console.log("压缩率：" + ~~(100 * (initSize - ndata.length) / initSize) + "%");
	        console.log(ndata)
	        return ndata;
	    },
	
	    
	   
	   
	}
	return frameScript;
})